<?php

namespace app\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Db;
use app\model\Site;
use think\facade\Session;
use think\facade\Request;
use app\utils\Utils;
use app\model\User;
use app\facade\Post;
use Throwable;
use app\utils\DataCache;

class Collection extends BaseController
{
    static public function get_collection($i = 1) // 根据合集id查询合集
    {
        $data = Db::table('site_collection')->where('id', $i)->select()->toArray();
        
        if(count($data)==0){
            return json(array('code'=>404,'msg'=>'该合集不存在'));
        }else if($data[0]['bad']==1){
            return json(array('code'=>403,'msg'=>'该合集违规'));
        }else{
            $return = array();
            $list = Db::table('site_collection_list')->where('collection_id', $i)->select()->toArray();
            foreach ($list as $key) {
                $posts = Db::connect()->table('site_post')->where('id', $key['post_id'])->select()->toArray(); // 查询帖子的数据
                if (count($posts) == 0) { // 没有数据
                    Db::table('site_collection_list')->where('post_id',$key['post_id'])->delete();
                } else {
                    if ($posts[0]['ban'] != 1) { // 帖子被封禁
                        $post_title = $posts[0]['title'];
                    }
                }
                $return[] = array("post_id"=>$key['post_id'],"post_title"=>$post_title);
            }

            return json(array('code'=>200,'msg'=>'success','name'=>$data[0]['title'],'data'=>$return));

        }
    
    }
    static public function get_user_collection($i = 1) // 获取用户合集列表
    {

    }
    static public function get_post_collection($i = 1) {//获取该帖子的所属合集
        $list = Db::table('site_collection_list')->where('post_id', $i)->select()->toArray();
        if(count($list)==0){
            return json(array('code'=>404,'msg'=>'该帖子不属于任何合集'));
        }
        $collection_id = $list[0]['collection_id'];
        $data = Db::table('site_collection')->where('id', $collection_id)->select()->toArray();
        
        if(count($data)==0){
            return json(array('code'=>404,'msg'=>'该合集不存在'));
        }else if($data[0]['bad']==1){
            return json(array('code'=>403,'msg'=>'该合集违规'));
        }
        else{
            $return = array();
            $list = Db::table('site_collection_list')->where('collection_id', $collection_id)->select()->toArray();
            foreach ($list as $key) {
                $posts = Db::connect()->table('site_post')->where('id', $key['post_id'])->select()->toArray(); // 查询帖子的数据
                if (count($posts) == 0) { // 没有数据
                    Db::table('site_collection_list')->where('post_id',$key['post_id'])->delete();
                } else {
                    if ($posts[0]['ban'] != 1) { // 帖子被封禁
                        $post_title = $posts[0]['title'];
                    }
                }
                $return[] = array("post_id"=>$key['post_id'],"post_title"=>$post_title);
            }

            return json(array('code'=>200,'msg'=>'success','name'=>$data[0]['title'],'data'=>$return));

        }
    }

}
